﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using BLL.AntiFake;
using System.Data;
using Newtonsoft.Json.Linq;
namespace NbSecurityAppManage.AntiFake
{
    public partial class Result : System.Web.UI.Page
    {
        DigitCodeBLL digitCodeBLL = new DigitCodeBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            string fwCode = Request.QueryString["fwcode"]; //防伪码
            string sycode = Request.QueryString["sycode"]; //溯源码

            if (string.IsNullOrWhiteSpace(fwCode))
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请输入防伪码！')</script>");
                body_1.InnerText = "";
                return;
            }
            if (string.IsNullOrWhiteSpace(sycode))
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请输入溯源码！')</script>");
                body_1.InnerText = "";
                return;
            }

            if (!IsPostBack)
            {
                ValidateFwCode(fwCode, sycode);
            }
            
            #region MyRegion

            //string code = string.Empty;
            //if (Request["code"]!=null)
            //code = Request["code"].ToString();
            //string type = Request["type"] != "" ? Request["type"] : "0";

            //if (!IsPostBack)
            //{
            //    FwCode_Verification(code);
            //}

            #endregion

        }

        #region 防伪查询

        public void ValidateFwCode(string fwCode, string sycode)
        {
            string result = "";
            string fwPart = sycode.Substring(0, 6);
            //查询ip和省市信息
            IPMethod me = new IPMethod();
            string ipAddress = me.GetHostAddress();
            JObject jo = me.IPGetProviceCity(ipAddress);
            string QueryProvince = jo["region"].ToString();
            string QueryCity = jo["city"].ToString();
            if (digitCodeBLL.TabExists(fwPart))
            {
                DataTable table = digitCodeBLL.GetModel(fwPart, fwCode, sycode, ipAddress, QueryProvince, QueryCity);
                if (table.Rows.Count <= 0)
                {
                    Response.Redirect("/AntiFake/errorResult.aspx");
                    result = "该防伪码不存在!";
                } 
                else
                {
                    div_code.InnerText = table.Rows[0]["Fw_CodeSecret"]!=DBNull.Value?table.Rows[0]["Fw_CodeSecret"].ToString():"";
                    div_prodoctName.InnerText = table.Rows[0]["ProductName"] != DBNull.Value? table.Rows[0]["ProductName"] .ToString():"";
                    string queryTime = string.Empty;
                    if (table.Rows[0]["QueryTime"]!=DBNull.Value)
                    {
                        queryTime = Convert.ToDateTime(table.Rows[0]["QueryTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                   }
                        div_queryTime.InnerText = queryTime;
                        div_queryCount.InnerText = table.Rows[0]["QueryCount"] == DBNull.Value ? "1" :(Convert.ToInt32(table.Rows[0]["QueryCount"].ToString())+1).ToString();
                    string queryCount = table.Rows[0]["QueryCount"].ToString();
                    if (string.IsNullOrEmpty(queryCount))
                    {
                        result = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！您所查询的是" + table.Rows[0]["EnterpriseName"] + "认证产品，是正牌产品的标识，请放心使用，当前为第 1 次查询，欢迎再次购买！";
                        div_imgzj.Style.Add("background-color","#79BC1F");
                        //修改首次查询日期和查询次数
                       digitCodeBLL.UpdateCount(fwPart, queryCount, table.Rows[0]["ID"].ToString(), "both");
                    }
                    else {
                        //只修改查询次数
                        result = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好!该防伪码于"+ queryTime+"被首次查询，谨防假冒！如有疑问请与当地厂商联系！";
                        img_zj.Src = "/imgs/fw/y2.png";
                        img_zj.Style.Add("width","45%");
                      //  div_imgzj.Style.Add("margin-buttom", "20px");
                        div_imgzj.Style.Add("background-color", "#F76A0E");
                        digitCodeBLL.UpdateCount(fwPart,queryCount, table.Rows[0]["ID"].ToString(), "count");
                    }

                    //查询记录

                }
            }
            else
            {
                Response.Redirect("/AntiFake/errorResult.aspx");
                result = "该防伪码不存在!";
            }

            this.labelResult.Text = result;

        }

        #endregion

        #region MyRegion

        //public void FwCode_Verification(string code)
        //{
        //    string tim = "第一次查询";
        //    fwwlCode.fwwlCode fwService = new fwwlCode.fwwlCode();

        //    //string str = new fwwlCode().FwCode_Verification(code, base.Request.UserHostAddress, 1);
        //    string str = fwService.FwCode_Verification(code, "192.168.1.1", 1);
        //    string str2 = "";
        //    string str3 = "";
        //    string str4 = "";
        //    string str5 = "";
        //    string[] strArray = str.Split(new char[] { '^' });
        //    str2 = strArray[0].ToString();
        //    str3 = strArray[1].ToString();
        //    str4 = strArray[2].ToString();
        //    str5 = strArray[3].ToString();
        //    string strTemp = string.Empty;
        //    StringBuilder sb = new StringBuilder();
        //    switch (str2)
        //    {
        //        case "1":
        //            strTemp = str3.Length <= 0 ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好!您所查询的是中国好粮油的生产的产品,是正牌产品的标识,请放心使用,当前为第一次查询,欢迎再次购买！" : str3;
        //            this.labelResult.Text = sb.Append(strTemp).ToString();
        //            time.Text = tim;
        //            return;
        //        case "2":
        //            tim = Convert.ToDateTime(str4).ToLongDateString() + Convert.ToDateTime(str5).ToLongTimeString();
        //            // this.message.Text = sb.Append(str3).ToString();
        //            strTemp = str3.Length <= 0 ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好!您所查询的是红星墨业有限公司生产的,是正牌产品的标识,请放心使用,欢迎再次购买!" : str3;
        //            this.message.Text = sb.Append(strTemp).ToString();
        //            time.Text = tim;
        //            return;
        //        case "0":
        //            this.message.Text = sb.Append("您所输入的密码为").Append(code).Append(str4).ToString();
        //            time.Text = "该防伪码不存在";
        //            return;
        //    }
        //    this.message.Text = sb.Append(str3).ToString();
        //}
        #endregion

    }
}